package com.wei.czz.framework.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.enums.ResultEnum;
import com.wei.czz.common.enums.blog.audit.AuditStatusEnum;
import com.wei.czz.common.exception.CzzException;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.framework.blog.dao.BlogAuditDao;
import com.wei.czz.framework.blog.entity.BlogAuditEntity;
import com.wei.czz.framework.blog.service.BlogAuditService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-11-29 20:24:48
 * className: BlogAuditServiceImpl
 * version: 1.0
 * description:
 */
@Service("blogAuditService")
public class BlogAuditServiceImpl extends ServiceImpl<BlogAuditDao, BlogAuditEntity> implements BlogAuditService {

    private static final Logger log = LoggerFactory.getLogger(BlogAuditServiceImpl.class);

    @Override
    public void saveOrEdit(BlogAuditEntity blogAudit) {
        // 获取操作用户
        String username = SecurityUtils.getOrDefaultUsername(StringUtils.EMPTY);
        Long userId = SecurityUtils.getOrDefaultUserId(CommonEnum.ZERO.getLongValue());
        Date date = new Date();

        if (Objects.isNull(blogAudit.getId())) {
            blogAudit.setDeleteStatus(CommonEnum.ZERO.getValue())
                    .setUpdateTime(date)
                    .setUpdateUser(StringUtils.EMPTY)
                    .setUpdateUserId(CommonEnum.ZERO.getLongValue())
                    .setCreateTime(date)
                    .setCreateUser(username)
                    .setCreateUserId(userId);

            int count = baseMapper.insert(blogAudit);
            log.info("保存博客审核记录完成。count:{}", count);
        } else {
            blogAudit.setUpdateTime(date)
                   .setUpdateUser(username)
                   .setUpdateUserId(userId);

            int count = baseMapper.updateById(blogAudit);
            log.info("修改博客审核记录完成。count:{}", count);
        }
    }

    @Override
    public BlogAuditEntity get(Long id) {
        BlogAuditEntity blogAudit = baseMapper.selectById(id);
        if (Objects.isNull(blogAudit)) {
            log.info("博客审核记录不存在");
            throw new CzzException(ResultEnum.NOT_FOUND.getCode(), "博客审核记录不存在，请确认");
        }
        if (CommonEnum.ONE.getValue().equals(blogAudit.getDeleteStatus())) {
            log.info("博客审核记录已删除");
            throw new CzzException(ResultEnum.NOT_FOUND.getCode(), "博客审核记录已删除，请确认");
        }
        return blogAudit;
    }

    @Override
    public List<BlogAuditEntity> getList(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            log.info("查询博客审核记录，传递的主键列表为空");
            return Collections.emptyList();
        }

        LambdaQueryWrapper<BlogAuditEntity> blogAuditLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogAuditLambdaQueryWrapper.in(BlogAuditEntity::getId, idList)
               .eq(BlogAuditEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<BlogAuditEntity> blogAuditList = baseMapper.selectList(blogAuditLambdaQueryWrapper);
        log.info("主键列表查询博客审核记录完成。list.size={}", blogAuditList.size());

        return blogAuditList;
    }

    @Override
    public List<BlogAuditEntity> findList(List<Long> blogIdList, List<Integer> typeList) {
        if (CollectionUtils.isEmpty(blogIdList)) {
            log.info("查询博客审核记录，传递的博客主键列表为空");
            return Collections.emptyList();
        }

        LambdaQueryWrapper<BlogAuditEntity> blogAuditLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogAuditLambdaQueryWrapper.in(BlogAuditEntity::getBlogId, blogIdList)
                .in(!CollectionUtils.isEmpty(typeList), BlogAuditEntity::getType, typeList)
                .eq(BlogAuditEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<BlogAuditEntity> blogAuditList = baseMapper.selectList(blogAuditLambdaQueryWrapper);
        log.info("查询博客审核记录完成。list.size={}", blogAuditList.size());

        return blogAuditList;
    }

    @Override
    public List<BlogAuditEntity> findUserWaitAuditList(List<Long> auditUserIdList) {
        if (CollectionUtils.isEmpty(auditUserIdList)) {
            log.info("查询用户待审核的博客审核记录，传递的审核用户主键列表为空");
            return Collections.emptyList();
        }

        LambdaQueryWrapper<BlogAuditEntity> blogAuditLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogAuditLambdaQueryWrapper.in(BlogAuditEntity::getAuditUserId, auditUserIdList)
                .eq(BlogAuditEntity::getAuditStatus, AuditStatusEnum.WAIT.getValue())
                .eq(BlogAuditEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<BlogAuditEntity> blogAuditList = baseMapper.selectList(blogAuditLambdaQueryWrapper);
        log.info("查询用户待审核的博客审核记录完成。list.size={}", blogAuditList.size());

        return blogAuditList;
    }

    @Transactional
    @Override
    public void batchUpdate(List<BlogAuditEntity> blogAuditList) {
        boolean bool = this.updateBatchById(blogAuditList);
        log.info("批量更新博客审核记录完成。bool:{}", bool);
    }

    @Override
    public void deleteWaitAudit(List<Long> blogIdList) {
        if (CollectionUtils.isEmpty(blogIdList)) {
            log.info("删除待审核的博客审核记录，传递的博客主键列表为空");
            return;
        }
        // 获取操作用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaUpdateWrapper<BlogAuditEntity> blogAuditLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        blogAuditLambdaUpdateWrapper.set(BlogAuditEntity::getDeleteStatus, CommonEnum.ONE.getValue())
                .set(BlogAuditEntity::getUpdateTime, date)
                .set(BlogAuditEntity::getUpdateUser, userPo.getUsername())
                .set(BlogAuditEntity::getUpdateUserId, userPo.getUserId());
        blogAuditLambdaUpdateWrapper.in(BlogAuditEntity::getBlogId, blogIdList)
              .eq(BlogAuditEntity::getAuditStatus, AuditStatusEnum.WAIT.getValue())
              .eq(BlogAuditEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 删除
        int count = baseMapper.update(blogAuditLambdaUpdateWrapper);
        log.info("删除待审核的博客审核记录完成。count={}", count);
    }
}